define([
        'app',
        './API'
    ],
    function(app) {
        'use strict';
        app.factory('CartAPI', ['API', '$rootScope', '$localForage', '$resource', '$interval',
            function(API, $rootScope, $localForage, $resource, $interval) {
                var _API = new API('cart');
                var server = _API.server;

                var cartList = [];
                var orderList = [];
                var _cart = 'cartList';
                var _order = 'orderList';

                function getLocalInfo(data) {
                    return $resource(server + '/local/info').get(data).$promise;
                }

                function getDetailList(data) {
                    return $resource(server + '/data/list').get(data).$promise;
                }

                function getCardCode(data){
                    return $resource(server + '/data/list').get(data).$promise;
                }

                return angular.extend(_API, {
                    addCart: function(product, options, callback) {
                        options = options || {};
                        var p = {
                            product: {
                                objectId: product.objectId,
                                __type: 'Pointer',
                                className: 'Product'
                            },
                            options: options
                        };
                        //如果已经登录,直接存进数据库
                        if ($rootScope.currentUser) {
                            p.user = {
                                objectId: $rootScope.currentUser.objectId,
                                __type: 'Pointer',
                                className: '_User'
                            };
                            _API.add(p).then(function(data) {
                                console.log(data);
                                if (typeof(callback) == 'function') callback();
                            })
                        } else {
                            $localForage.getItem(_cart).then(function(data) {
                                if (typeof(callback) == 'function') callback();
                                if (!data) {
                                    data = [];
                                }
                                data.push(p);
                                $localForage.setItem(_cart, data).then(function(data){
                                    console.log(data);
                                },function(data){
                                    console.log(data);
                                    alert("无法为您添加到购物车，请登录或退出无痕模式。");
                                    return false;
                                });
                            }, function(data) {
                                console.log(data);
                                alert("无法为您添加到购物车，请登录或退出无痕模式。");
                            });

                        }
                    },
                    getCart: function() {
                        //$localForage.removeItem(_cart);
                        //直接取缓存
                        return $localForage.getItem(_cart).then(function(data) {
                            //未登录调用getLocalInfo
                            if (!$rootScope.currentUser) {
                                if (data) {
                                    return getLocalInfo({
                                        localCart: new Array(data)
                                    }).then(function(data) {
                                        console.log(data);
                                        orderList = data;
                                        return data;
                                    });
                                }
                            } else {
                                //已登录登录调用getLocalInfo
                                if (data) { //如果有缓存 提交缓存
                                    console.log(data);
                                    return getDetailList({
                                        localCart: new Array(data)
                                    }).then(function(data) {
                                        if (data) {
                                            console.log(data);
                                            $localForage.removeItem(_cart);
                                            orderList = data;
                                            return data;
                                        }
                                    });
                                } else {
                                    return getDetailList().then(function(data) {
                                        if (data) {
                                            orderList = data;
                                            return data;
                                        }
                                    });
                                }
                            }
                        });
                    },
                    linkToOrder: function(p) {
                        return getLocalInfo({
                            localCart: new Array(p)
                        }).then(function(data) {
                            if (data) {
                                console.log(data);
                                return data;
                            }
                        });
                    },
                    setOrder: function(index, select) {
                        //如果select 为该项设置isSelected属性
                        orderList.data[index].isSelected = select;
                        $localForage.setItem(_order, orderList);
                    },
                    getOrder: function() {
                        //结算 选中的
                        return $localForage.getItem(_order).then(function(data) {
                            if (data) {
                                return data;
                            }
                        })
                    },
                    getCard: function(){
                        return getCardCode().then(function(data){
                            if(data){
                                return data;
                            }
                        });
                    }
                });
            }
        ]);
    });
